package com.bandi.tharshesnows.geolocating;

import java.io.IOException;

import com.bandi.tharshesnows.model.Location;
import com.bandi.tharshesnows.pics.PictureStorage;

public class StaticMaps {

  // private static final String STATIC_MAP_PATTERN =
  // "http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom=10&maptype=satellite&markers=color:blue|{0},{1}&size=400x150&sensor=false";
  public static final String STATIC_MAP_PATTERN = "http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom={2}&markers=color:blue|{0},{1}&size={3}x{4}&sensor=false";

  public static String getMapPath(String name, int zoom, int width, int height, Location center, Location... locations)
      throws IOException {
    String picName = name.replace(", ", "_").trim() + "_z" + zoom + ".jpg";
    StringBuilder builder = new StringBuilder();
    builder.append("http://maps.google.com/maps/api/staticmap?");
    builder.append("center=");
    builder.append(center.getLatitude());
    builder.append(",");
    builder.append(center.getLongitude());
    builder.append("&zoom=");
    builder.append(zoom);
    builder.append("&size=");
    builder.append(width);
    builder.append("x");
    builder.append(height);
    builder.append("&sensor=false");
    builder.append("&scale=2");
    builder.append("&markers=color:blue");
    if (locations != null && locations.length > 0) {
      for (Location location : locations) {
        builder.append("|");
        builder.append(location.getLatitude());
        builder.append(",");
        builder.append(location.getLongitude());
      }
    }
    else {
      builder.append("|");
      builder.append(center.getLatitude());
      builder.append(",");
      builder.append(center.getLongitude());
    }
    PictureStorage.getInstance().storeImage(builder.toString(), picName);
    return picName;
  }

}
